class Graph{
    constructor(isDirctive=false){ //无向图
        this.isDirctive = isDirctive;
        this.vertices = [];  //顶点
        this.edges = new Map(); //边
    }

    //添加顶点
    addVertex(v){
        if(!this.vertices.includes(v)){
            this.vertices.push(v);
            this.edges.set(v,[]);
        }
    }

    //添加边
    addEdge(v,m){ // v--->m  边
        //1.判断顶点是否添加
        if(!this.vertices.includes(v)){
            this.addVertex(v)
        }

        if(!this.vertices.includes(m)){
            this.addVertex(m)
        }

        this.edges.get(v).push(m);

    }

    //获取顶点
    getVertices(){
        return this.vertices
    }

    getEdges(){
        return this.edges
    }

    toString(){
        let str = '';
        for(let i = 0;i<this.vertices.length;i++){
            //0
            str += `${this.vertices[i]}--->`;
            let edgeList = this.edges.get(this.vertices[i]);
            for(let j = 0;j<edgeList.length;j++){
                str += `${edgeList[j]} `
            }
            str += '\n';
        }
        return str
    }

    //打印相邻顶点的个数
    printVertex(){
        let str='';
        for(let i = 0;i<this.vertices.length;i++){
            //0
            str += `${this.vertices[i]}--->`;
            let edgeList = this.edges.get(this.vertices[i]);
            str+=edgeList.length;
            str+='\n';
        }   
        return str
    }
}

let g = new Graph(true);

g.addVertex(0);
g.addVertex(1);
g.addVertex(2);
g.addVertex(3);
g.addVertex(4);
g.addVertex(5);
g.addVertex(6);
g.addVertex(7);
g.addVertex(8);
g.addVertex(9);
g.addVertex(10);
g.addVertex(11);
g.addVertex(12);

g.addEdge(0,5);// v--->m  边
g.addEdge(0,1);

g.addEdge(2,0);// v--->m  边
g.addEdge(2,3);
g.addEdge(3,2);
console.log(g.printVertex());

